package com.shucoin.www.tycoin.ui.home;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.shucoin.www.tycoin.R;
import com.shucoin.www.tycoin.ui.home.c2c.TradeCoinFt;
import com.shucoin.www.tycoin.ui.home.info.ParentInfoFt;
import com.shucoin.www.tycoin.ui.home.me.AboutMeFt;
import com.shucoin.www.tycoin.ui.home.trade.ParentTradeFt;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.nav_info)
    RadioButton navInfo;
    @BindView(R.id.nav_trade)
    RadioButton navTrade;
    @BindView(R.id.nav_history)
    RadioButton navHistory;
    @BindView(R.id.nav_c2c)
    RadioButton navC2c;
    @BindView(R.id.nav_menu)
    RadioGroup navMenu;
    @BindView(R.id.frame)
    FrameLayout frame;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private ParentInfoFt infoFt;
    private ParentTradeFt tradeFt;
    private AboutMeFt aboutFt;
    private TradeCoinFt coinFt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navMenu.setOnCheckedChangeListener(this);
        showFragment(0);
    }



    public void showFragment(int checkedId){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft= fragmentManager.beginTransaction();
        switch (checkedId){
            case R.id.nav_info:
            case 0:
                if (infoFt==null){
                    infoFt=new ParentInfoFt();
                    ft.add(R.id.frame,infoFt);
                }else {
                    ft.show(infoFt);
                }
                break;
            case R.id.nav_trade:
            case 1:
                if (tradeFt==null){
                    tradeFt=new ParentTradeFt();
                    ft.add(R.id.frame,tradeFt);
                }else {
                    ft.show(tradeFt);
                }
                break;
            case R.id.nav_history:
            case 2:
                if (coinFt==null){
                    coinFt=new TradeCoinFt();
                    ft.add(R.id.frame,coinFt);
                }else {
                    ft.show(coinFt);
                }

                break;
            case R.id.nav_c2c:
            case 3:
                if (aboutFt==null){
                    aboutFt=new AboutMeFt();
                    ft.add(R.id.frame,aboutFt);
                }else {
                    ft.show(aboutFt);
                }
                break;
        }
        ft.commitNow();
    }
    public void hideFragment(){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft= fragmentManager.beginTransaction();
        if (infoFt!=null)
            ft.hide(infoFt);
        if (tradeFt!=null)
            ft.hide(tradeFt);
        if (coinFt!=null)
            ft.hide(coinFt);
        if (aboutFt!=null)
            ft.hide(aboutFt);
        ft.commitNow();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }




    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.nav_camera:
                break;
        }
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        hideFragment();
        showFragment(checkedId);
    }
}
